package com.xywy.bloodandroid.act;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.adapter.QuestOnlineAnswerAdapter;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.bean.OnlineQuestAnswerData;
import com.xywy.bloodandroid.util.Constants;
import com.xywy.bloodandroid.util.JsonUtil;
import com.xywy.bloodandroid.util.LogUtil;
import com.xywy.bloodandroid.util.UIUtil;
import com.xywy.bloodandroid.vollery.GetRequest;
import com.xywy.bloodandroid.vollery.OnVolleyResponseListener;

/**
 * 回答在线问题展示页面；
 */
public class QuestOnlineAnswerDetailAct extends BaseActivity {
	protected static final String TAG = "QuestOnlineAnswerDetailAct";
	private QuestOnlineAnswerAdapter adapter; // 医生回答Adapter

	private ListView lv_answer; // 医生回答列表；
	private RelativeLayout display_rel; // 展开问题按钮；
	private RelativeLayout rl_question_detail; // 问题详情展示；
	private RelativeLayout left_img; // 返回按钮

	private TextView tv_question_title;// 提问的问题；
	private TextView tv_sex;// 问题： 性别；
	private TextView tv_age;// 问题： 年龄；
	private TextView tv_date;// 问题： 时间；
	private TextView tv_desc;// 问题： 描述；
	private TextView tv_question_content;// 问题： 性别；
	private TextView middle_title; // 标题

	private String art_id = "0"; // 上个页面传递过来的文章id
	private List<OnlineQuestAnswerData> answerDatas = new ArrayList<OnlineQuestAnswerData>(); // 医生回答数据；

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.act_quest_answer);
		answerDatas.clear();
		getPreActData();
		lv_answer = (ListView) findViewById(R.id.lv_answer);
		display_rel = (RelativeLayout) findViewById(R.id.display_rel);
		rl_question_detail = (RelativeLayout) findViewById(R.id.rl_question_detail);
		tv_question_title = (TextView) findViewById(R.id.tv_question_title);
		tv_sex = (TextView) findViewById(R.id.tv_sex);
		tv_age = (TextView) findViewById(R.id.tv_age);
		tv_date = (TextView) findViewById(R.id.tv_date);
		tv_desc = (TextView) findViewById(R.id.tv_desc);
		tv_question_content = (TextView) findViewById(R.id.tv_question_content);

		// titile
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText("问题回复");
		// back
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		left_img.setOnClickListener(this);

		rl_question_detail.setOnClickListener(this);

		getData();
	}

	/**
	 * 获取上个页面传递过来的数据
	 */
	private void getPreActData() {
		Intent intent = getIntent();
		art_id = intent.getStringExtra("art_id");
	}

	@Override
	protected void initListener() {
		display_rel.setOnClickListener(this);
	}

	@Override
	protected void initData() {
	}

	/**
	 * 从服务器获取回答的数据；
	 */
	private void getData() {
		String url = "http://api.wws.xywy.com/index.php?&tag=" + Constants.tag
				+ "&sign=" + Constants.Sign + "&act=" + "club" + "&fun="
				+ "Club_quesDetail";
		// String url = Constant.getUrl("club", "Club_quesDetail") + "&art_id="
		// + art_id;
		GetRequest<String> getRequest = new GetRequest<String>(url,
				String.class, new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {
						UIUtil.DissDialog(context, waittingDialog);
						UIUtil.showToast(QuestOnlineAnswerDetailAct.this,
								getResources().getString(R.string.network_slow));
					}

					@Override
					public void onSuccess(String response) {
						UIUtil.DissDialog(context, waittingDialog);
						try {
							ParserData(response);
						} catch (JSONException e) {
							LogUtil.d(TAG, "解析异常");
							e.printStackTrace();
						}

					}

				});
		getRequest.setIsParseJson(false);
		executeRequest(getRequest);

	}

	/**
	 * 解析从服务器上获取的数据
	 * 
	 * @param response
	 * @throws JSONException
	 */
	@SuppressWarnings("unchecked")
	private void ParserData(String response) throws JSONException {
		JSONObject jsonObject = new JSONObject(response);
		int state = jsonObject.getInt("state");
		if (state == 200) {
			if (jsonObject.has("data")
					&& jsonObject.optString("data").length() > 0) {
				String data = jsonObject.optString("data");
				/*------------------------病情描述--------------------------*/
				JSONObject jsonObjectInside = new JSONObject(data);
				String question = jsonObjectInside.getString("question");
				String title = jsonObjectInside.getString("title");
				tv_question_title.setText(title + "");
				String detail = jsonObjectInside.getString("detail");
				tv_question_content.setText(detail + "");
				String sex = jsonObjectInside.getString("sex");
				tv_sex.setText(sex + "");
				String age = jsonObjectInside.getString("age");
				tv_age.setText(age + "");
				String askTime = jsonObjectInside.getString("askTime");
				tv_date.setText("发表于：" + askTime);
				String url = jsonObjectInside.getString("url");
				/*------------------------医生回答--------------------------*/
				String repliedList = jsonObjectInside.getString("repliedList");

				answerDatas.addAll((List<OnlineQuestAnswerData>) JsonUtil
						.parseJsonToList(repliedList,
								new TypeToken<List<OnlineQuestAnswerData>>() {
								}.getType()));
				adapter = new QuestOnlineAnswerAdapter(
						QuestOnlineAnswerDetailAct.this, answerDatas, lv_answer);
				lv_answer.setAdapter(adapter);

			}
		}
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {

		case R.id.display_rel:
			// 如果病情描述没有打开 ，点击之后打开 ；
			if (rl_question_detail.getVisibility() == View.VISIBLE) {
				rl_question_detail.setVisibility(View.GONE);
			} else {

				rl_question_detail.setVisibility(View.VISIBLE);

				// ScaleAnimation sa = new ScaleAnimation(1f, 1f, 0f, 1f,
				// Animation.RELATIVE_TO_SELF, 0f,
				// Animation.RELATIVE_TO_SELF, 0f);
				// sa.setDuration(500);
				// rl_question_detail.setAnimation(sa);
				// rl_question_detail.startAnimation(sa);

				TranslateAnimation ta = new TranslateAnimation(
						Animation.RELATIVE_TO_SELF, 0,
						Animation.RELATIVE_TO_SELF, 0,
						Animation.RELATIVE_TO_SELF, -1,
						Animation.RELATIVE_TO_SELF, 0);
				// TranslateAnimation ta = new TranslateAnimation(0, 0, -300,
				// 0);
				ta.setDuration(500);
				rl_question_detail.setAnimation(ta);
				rl_question_detail.startAnimation(ta);

			}

			break;
		case R.id.left_img:
			this.finish();
			break;
		}
	}
}
